<?php

namespace Liuhelong\LaravelAdmin\Wechat\Models;

use Illuminate\Database\Eloquent\Model;
use Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Cache;
use Storage;

class WechatOffiaccountCustomerService extends Model
{
	protected $primaryKey = 'kf_account';
	public $incrementing = false;
	private $wechat;
	
	
	
	public function __construct(array $attributes = []){
		parent::__construct($attributes);
		$this->wechat = app('liuhelong_wechat_service');
	}
	
	
	public function paginate()
    {
        $perPage = Request::get('per_page', 20);
        $page = Request::get('page', 1);
        $start = ($page-1)*$perPage;

        $data = $this->wechat->handle('customer_service','list');
        $cs_online = collect($this->wechat->handle('customer_service','online')['kf_online_list'])->pluck('kf_account')->all();
        foreach($data['kf_list'] as $k=>$kf){
            $data['kf_list'][$k]['online'] = (in_array($kf['kf_account'],$cs_online))?true:false;
        }
        Cache::put('customer_services',collect($data['kf_list'])->keyBy($this->primaryKey)->toArray());
        
		$list = static::hydrate(array_slice($data['kf_list'],$start,$perPage));
        //dd($list);
        $paginator = new LengthAwarePaginator($list, count($data['kf_list']), $perPage);

        $paginator->setPath(url()->current());

        return $paginator;
    }

    public static function with($relations)
    {
        return new static;
    }
	
	public function orderByRaw()
    {
		return $this;
    }
	protected function findOrFail($id)
	{
		$customer_services = Cache::get('customer_services',false);
		return static::newFromBuilder($customer_services[$id]??[]);
	}
	// 保存提交的form数据
	public function save(array $options = [])
	{
		$attributes = $this->getAttributes();
	    if($this->isDirty('kf_account')){
		    $this->wechat->handle('customer_service','create',[$attributes['kf_account'],$attributes['kf_nick']]);
	    }else{
	        $this->wechat->handle('customer_service','update',[$attributes['kf_account'],$attributes['kf_nick']]);
	    }
		//$path = Storage::disk('public')->url(Request::file('kf_headimgurl')->store('wechat_customer_service_head_imgs','public'));
		
		
	    if($this->isDirty('kf_headimgurl')){
    		$path = config('filesystems.disks.local.root').DIRECTORY_SEPARATOR.Request::file('kf_headimgurl')->store('wechat_customer_service_head_imgs');
    		
    		// save $attributes
    		
    		$this->wechat->handle('customer_service','setAvatar',[$attributes['kf_account'],$path]);
	    }
		
	}
	
	public function delete()
	{
		$this->wechat->handle('customer_service','delete',[$this->getKey()]);
		
	}
    // 覆盖`orderBy`来收集排序的字段和方向
    public function orderBy($column, $direction = 'asc')
    {

    }

    // 覆盖`where`来收集筛选的字段和条件
    public function where($column, $operator = null, $value = null, $boolean = 'and')
    {
    }
    public function __call($method, $parameters){
        if($method=='findOrFail'){
            return $this->$method(...$parameters);
        }
        return parent::__call($method, $parameters);
    }
    
}